package logic

import (
	"context"
	"errors"
	"user_srv/model/mysql"
	"user_srv/pkg"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameLogic) RealName(in *user.RealNameRequest) (*user.RealNameResponse, error) {
	// todo: add your logic here and delete this line
	ur := mysql.RealName{}
	err := ur.RealNameFind(in.Name, in.IdCard)
	if err != nil {
		logx.Error("认证失败1")
		return nil, errors.New("实名认证失败1")
	}
	if ur.ID > 0 {
		logx.Error("认证失败2")
		return nil, errors.New("实名认证失败2")
	}
	if !pkg.RealName(in.Name, in.IdCard) {
		logx.Error("认证失败3")
		return nil, errors.New("实名认证失败3")
	}
	ur.Userid = int(in.Userid)
	ur.Name = in.Name
	ur.IdCard = in.IdCard
	err = ur.RealNameCreate()
	if err != nil {
		logx.Error("认证失败4")
		return nil, errors.New("实名认证失败4")
	}
	return &user.RealNameResponse{RealId: int64(ur.ID)}, nil
}
